package com.klxedu.ms.gateway.security.authentication.impl;

import java.util.ArrayList;

import com.klxedu.ms.gateway.core.json.BaseJsonObject;
import com.klxedu.ms.gateway.security.authentication.IAuthenticateRoles;
import com.klxedu.ms.gateway.security.authentication.IAuthenticatedUser;
import com.klxedu.ms.gateway.security.client.RoleAuthFeignClient;
import com.klxedu.ms.gateway.utils.SpringBeanUtils;

/**
 * 用户名登录认证角色获取
 * 
 * @author songhuiqing
 *
 */
public class UserNameAuthenticateRolesImpl implements IAuthenticateRoles {

	@Override
	public AuthorizedRoles doDetails(IAuthenticatedUser user) {
		AuthorizedRoles authorizedDetails = new AuthorizedRoles();
		ArrayList<String> userRoles = new ArrayList<>();
		String entityID = user.getEntityID();
		String[] sysIdentityCodes = user.getSysIdentityCodes();
		if (sysIdentityCodes != null) {
			for (String identity : sysIdentityCodes) {
				userRoles.add(identity);// 将身份作为角色添加
			}
		}
		RoleAuthFeignClient roleAuthClient = SpringBeanUtils.getBean(RoleAuthFeignClient.class);
		BaseJsonObject<String[]> roleResult = roleAuthClient.listRoleIDByUserId(entityID);
		if (roleResult.getData() != null) {
			for (String roleID : roleResult.getData()) {
				userRoles.add(roleID);
			}
		}
		authorizedDetails.setUserRoles(userRoles);
		return authorizedDetails;
	}

}
